package com.wukang.photovoltaicinspectionpictures.ui.main

import android.content.Intent
import android.view.View
import android.widget.PopupMenu
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import com.wukang.photovoltaicinspectionpictures.R
import com.wukang.photovoltaicinspectionpictures.base.BaseActivity
import com.wukang.photovoltaicinspectionpictures.databinding.ActivityMainBinding
import com.wukang.photovoltaicinspectionpictures.ui.dialog.CustomDialog
import com.wukang.photovoltaicinspectionpictures.ui.dialog.TaskBottomDialog
import com.wukang.photovoltaicinspectionpictures.ui.setting.SettingActivity
import com.wukang.photovoltaicinspectionpictures.ui.task.TaskActivity
import com.wukang.photovoltaicinspectionpictures.utils.onDebounceClick
import com.wukang.photovoltaicinspectionpictures.utils.toast


/**
 * 主界面
 * @author wukang
 */
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun initViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)

    override fun initViewModel(): MainViewModel = ViewModelProvider(this)[MainViewModel::class.java]

    override fun initActivity() {

    }

    override fun ActivityMainBinding.initView() {
        // 设置
        ivSetting.onDebounceClick { showSettingPopWindow(it) }
        // 新任务
        tvTaskNew.onDebounceClick { startActivity(Intent(this@MainActivity, TaskActivity::class.java)) }
        // 选择任务
        tvTaskSelect.onDebounceClick { showBottomTaskDialog(it) }
        // 开始
        btFuncStart.onDebounceClick {
            (it as TextView).text.toast(it.context)
        }
    }

    // 设置弹窗
    private fun showSettingPopWindow(view: View) {
        PopupMenu(this, view).apply {
            menuInflater.inflate(R.menu.main_setting, menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    // 出厂设置
                    R.id.main_reset -> {
                        startActivity(Intent(this@MainActivity, SettingActivity::class.java))
                        true
                    }
                    // 日志上传
                    R.id.main_logger -> {
                        mModel.uploadLogger()
                        true
                    }
                    // 退出登录
                    R.id.main_logout -> {
                        it.title.toast(view.context)
                        true
                    }
                    // 其他情况为未点击
                    else -> false
                }
            }
            show()
        }
    }

    // 底部任务弹窗
    private fun showBottomTaskDialog(view: View) {
        TaskBottomDialog(view.context).apply {
            setTaskSelectListener {
                it.toast(view.context)
            }
            show()
        }
    }

    // wifi不可用确认框
    private fun showWifiUnavailable(callback: () -> Unit) {
        CustomDialog(
            context = this,
            titleRes = R.string.main_dialog_title,
            msgRes = R.string.main_dialog_msg,
            negativeRes = R.string.btn_cancel,
            positiveRes = R.string.main_dialog_continue,
            positiveListener = { callback() }
        ).also { dialogDispose.add(it) }.show()
    }
}